// pages/repair/repair.js
import api from "../../../utils/request"
Page({
    data: {
      activeTab: "all",
      repairs: [
        {
          id: "1",
          repairNumber: "WX202312010001",
          repairTime: "2023-12-01",
          repairTypeName: "水电维修",
          typeTag: "primary",
          problemDescription: "厨房水龙头漏水，需要更换密封圈或水龙头。",
          status: 0,
          statusText: "待派单",
          statusType: "warning",
          houseAddress: "城市花园1栋1单元1层101",
          staffName: "",
        },
        {
          id: "2",
          repairNumber: "WX202311280002",
          repairTime: "2023-11-28",
          repairTypeName: "门窗维修",
          typeTag: "success",
          problemDescription: "卧室窗户无法正常关闭，可能是滑轨出现问题。",
          status: 1,
          statusText: "待接单",
          statusType: "primary",
          houseAddress: "城市花园1栋2单元2层201",
          staffName: "王师傅",
        },
        {
          id: "3",
          repairNumber: "WX202311250003",
          repairTime: "2023-11-25",
          repairTypeName: "家电维修",
          typeTag: "danger",
          problemDescription: "空调不制冷，需要检查是否缺氟或有其他故障。",
          status: 2,
          statusText: "已完成",
          statusType: "success",
          houseAddress: "城市花园2栋1单元3层301",
          staffName: "李师傅",
        },
      ],
      statusMap: { // 状态映射
        0: { text: "待派单", type: "warning" },
        1: { text: "待接单", type: "primary" },
        2: { text: "已完成", type: "success" },
      },
      allRepairs: [], // 存储所有数据
    },
  
    onLoad() {
      this.setData({
        allRepairs: this.data.repairs,
      })
      this.loadRepairs()
    },
  
    // 加载报修数据
    loadRepairs() {
      wx.showLoading({
        title: "加载中...",
      })
  
      // 模拟API请求
      setTimeout(async() => {
        try {
            // 实际项目中这里应该是API请求
            const result = await api.get("/property/repairRequest/myList");
            if (result.code === 200) {
              const processedData = result.data.map((item) => ({
                id: item.id,
                repairNumber: item.repairNumber,
                repairTime: item.repairTime,
                repairTypeName: item.repairTypeName,
                typeTag: item.repairTypeId, // 假设后端返回的 repairTypeId 是一个标识
                problemDescription: item.problemDescription,
                status: item.status,
                statusText: this.data.statusMap[item.status]?.text || "未知状态",
                statusType: this.data.statusMap[item.status]?.type || "default",
                houseAddress: item.houseAddress,
                staffName: item.staffName,
              }));
    
              this.setData({
                repairs: processedData,
                allRepairs: processedData,
              });
            } else {
              wx.showToast({
                title: result.msg || "加载失败",
                icon: "none",
              });
            }
          } catch (error) {
            console.error("加载报修数据失败", error);
            wx.showToast({
              title: "加载失败，请稍后再试",
              icon: "none",
            });
          }
          wx.hideLoading();
      }, 500)
    },
  
    // 标签页切换
    onTabChange(e) {
      const { name } = e.detail
      this.setData({ activeTab: name })
      this.filterRepairs(name)
    },
  
    // 筛选报修
    filterRepairs(status) {
      const { allRepairs } = this.data
      let filteredRepairs = allRepairs
  
      if (status !== "all") {
        filteredRepairs = allRepairs.filter((item) => item.status.toString() === status)
      }
  
      this.setData({
        repairs: filteredRepairs,
      })
    },
  
    // 点击报修项
    onItemClick(e) {
      const { id } = e.currentTarget.dataset
      wx.navigateTo({
        url: `/pages/property/repair-detail/repair-detail?id=${id}`,
      })
    },
  
    // 提交报修
    onSubmitRepair() {
      wx.navigateTo({
        url: "/pages/property/repair-form/repair-form",
      })
    },
  
    // 下拉刷新
    onPullDownRefresh() {
      this.loadRepairs()
      setTimeout(() => {
        wx.stopPullDownRefresh()
      }, 1500)
    },
  })
  